﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Cash
    {
        public Cash()
        { }
        public int Add(Model.Cash model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@StarPrice", SqlDbType.Decimal,9),
					new SqlParameter("@OutPrice", SqlDbType.Decimal,9),
					new SqlParameter("@IntoPrice", SqlDbType.Decimal,9),
					new SqlParameter("@LastPrice", SqlDbType.Decimal,9),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@Inputtime", SqlDbType.DateTime),
					new SqlParameter("@Marks", SqlDbType.NVarChar),
					new SqlParameter("@shuoming", SqlDbType.NVarChar,500)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Time;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.StarPrice;
            parameters[5].Value = model.OutPrice;
            parameters[6].Value = model.IntoPrice;
            parameters[7].Value = model.LastPrice;
            parameters[8].Value = model.UserName;
            parameters[9].Value = model.InputUserId;
            parameters[10].Value = model.Inputtime;
            parameters[11].Value = model.Marks;
            parameters[12].Value = model.shuoming;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Cash_ADD", parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Cash model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@StarPrice", SqlDbType.Decimal,9),
					new SqlParameter("@OutPrice", SqlDbType.Decimal,9),
					new SqlParameter("@IntoPrice", SqlDbType.Decimal,9),
					new SqlParameter("@LastPrice", SqlDbType.Decimal,9),
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@Inputtime", SqlDbType.DateTime),
					new SqlParameter("@Marks", SqlDbType.NVarChar),
					new SqlParameter("@shuoming", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Time;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.StarPrice;
            parameters[5].Value = model.OutPrice;
            parameters[6].Value = model.IntoPrice;
            parameters[7].Value = model.LastPrice;
            parameters[8].Value = model.UserName;
            parameters[9].Value = model.InputUserId;
            parameters[10].Value = model.Inputtime;
            parameters[11].Value = model.Marks;
            parameters[12].Value = model.shuoming;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Cash_Update", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Cash_Delete", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public Model.Cash GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Cash model = new Model.Cash();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Cash_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Time"].ToString() != "")
                {
                    model.Time = DateTime.Parse(ds.Tables[0].Rows[0]["Time"].ToString());
                }
                model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                if (ds.Tables[0].Rows[0]["StarPrice"].ToString() != "")
                {
                    model.StarPrice = decimal.Parse(ds.Tables[0].Rows[0]["StarPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OutPrice"].ToString() != "")
                {
                    model.OutPrice = decimal.Parse(ds.Tables[0].Rows[0]["OutPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IntoPrice"].ToString() != "")
                {
                    model.IntoPrice = decimal.Parse(ds.Tables[0].Rows[0]["IntoPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastPrice"].ToString() != "")
                {
                    model.LastPrice = decimal.Parse(ds.Tables[0].Rows[0]["LastPrice"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                if (ds.Tables[0].Rows[0]["InputUserId"].ToString() != "")
                {
                    model.InputUserId = int.Parse(ds.Tables[0].Rows[0]["InputUserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Inputtime"].ToString() != "")
                {
                    model.Inputtime = DateTime.Parse(ds.Tables[0].Rows[0]["Inputtime"].ToString());
                }
                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                model.shuoming = ds.Tables[0].Rows[0]["shuoming"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Time,Code,Type,StarPrice,OutPrice,IntoPrice,LastPrice,UserName,InputUserId,Inputtime,Marks,shuoming ");
            strSql.Append(" FROM Cash ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }

    }
}
